<template>
  <div>
    <!-- 行内表单
      1.inline:表示为行内表单,一行内可以放多个元素
      2.placeholder:提示信息
      3.label:下拉框的值
      4.el-select中的v-model收集到的是el-option中的value值
       -->
    <el-form :inline="true" class="demo-form-inline" :model="cForm">
      <el-form-item label="一级分类">
        <el-select
          placeholder="请选择"
          value=""
          v-model="cForm.category1ID"
          @change="handler1"
          :disabled="show"
        >
          <el-option
            :label="c1.name"
            :value="c1.id"
            v-for="(c1, index) in list1"
            :key="c1.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          :disabled="show"
          placeholder="请选择"
          value=""
          v-model="cForm.category2ID"
          @change="handler2"
        >
          <el-option
            :label="c2.name"
            v-for="(c2, index) in list2"
            :key="c2.id"
            :value="c2.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          :disabled="show"
          placeholder="请选择"
          value=""
          v-model="cForm.category3ID"
          @change="handler3"
        >
          <el-option
            v-for="(c3, index) in list3"
            :key="c3.id"
            :label="c3.name"
            :value="c3.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {},
  name: "CategorySelect",
  data() {
    return {
      // 一级分类的数据
      list1: [],
      list2: [],
      list3: [],
      //收集相应的一级,二级,三级分类的ID
      cForm: {
        category1ID: "",
        category2ID: "",
        category3ID: "",
      },
    };
  },
  props: ["show"],
  computed: {},
  created() {},
  mounted() {
    //   由于一级分类是默认就有数据的,所以挂载完毕时就需要获取到一级分类的数据
    this.getCategory1List();
  },
  methods: {
    async getCategory1List() {
      let result = await this.$API.Attr.reqCategory1List();
      if (result.code == 200) {
        this.list1 = result.data;
      }
    },
    async handler1() {
      // 当一级分类选择的数据发生变化时,需要清除二级,三级分类的数据:
      this.list2 = [];
      this.list3 = [];
      this.cForm.category2ID = "";
      this.cForm.category3ID = "";
      const { category1ID } = this.cForm;
      this.$emit("getCategoryID", {
        categoryID: category1ID,
        level: 1,
      });
      let result = await this.$API.Attr.reqCategory2List(
        this.cForm.category1ID
      );

      if (result.code == 200) {
        this.list2 = result.data;
        return "ok";
      } else {
        return Promise.reject(new Error("数据接收失败"));
      }
    },
    async handler2() {
      // 当二级分类选择的数据发生变化时,三级分类也要跟着发生变化
      this.list3 = [];
      this.cForm.category3ID = "";
      const { category2ID } = this.cForm;
      this.$emit("getCategoryID", {
        categoryID: category2ID,
        level: 2,
      });
      let result = await this.$API.Attr.reqCategory3List(
        this.cForm.category2ID
      );

      if (result.code == 200) {
        this.list3 = result.data;
        return "ok";
      } else {
        return Promise.reject(new Error("数据接收失败"));
      }
    },
    handler3() {
      const { category3ID } = this.cForm;
      this.$emit("getCategoryID", {
        categoryID: category3ID,
        level: 3,
      });
    },
  },
  components: {},
};
</script>

<style scoped lang="scss">
</style>
